"use strict";
cc._RF.push(module, '7b8ec0+H3dGYZswpYiOozR/', 'PinYinSprite');
// PinYinSprite.js

"use strict";

var py = require('PinYin');

var phonetic_symbol = require('phonetic-symbol');

function debounce(func, wait, immediate) {
  var timeout;
  return function () {
    var context = this,
        args = arguments;

    var later = function later() {
      timeout = null;
      if (!immediate) func.apply(context, args);
    };

    var callNow = immediate && !timeout;
    clearTimeout(timeout);
    timeout = setTimeout(later, wait);
    if (callNow) func.apply(context, args);
  };
}

cc.Class({
  "extends": cc.Component,
  editor: {
    executeInEditMode: true,
    menu: 'PinYin/PinYinSprite'
  },
  properties: {
    pinyinString: {
      // default:'d',
      tooltip: '汉字字符',
      get: function get() {
        return this._dataID;
      },
      set: function set(val) {
        if (this._dataID !== val) {
          this._dataID = val;

          if (CC_EDITOR) {
            this._debouncedUpdateLabel();
          } else {
            this.fetchRender();
          }
        }
      }
    },
    _dataID: '',
    PinyinSpriteFrames: [cc.SpriteAtlas],
    spriteframeGroup: {
      "default": 0,
      tooltip: '默认纹理集组号'
    },
    spriteframe_prefix: {
      "default": 'image_',
      tooltip: '纹理集名称前缀'
    }
  },
  onLoad: function onLoad() {
    if (CC_EDITOR) {
      this._debouncedUpdateLabel = debounce(this.updateLabel, 200);
    }

    this.fetchRender(); // this.psf = this.PinyinSpriteFrames[this.spriteFrameGroup];
  },
  fetchRender: function fetchRender() {
    var layout = this.node.getComponent(cc.Layout);

    if (layout) {
      this.layout = layout;
      this.updateLabel();
      return;
    }
  },
  setString: function setString(str) {
    this.pinyinString = str;
  },
  resetSpriteRender: function resetSpriteRender(arr) {
    var _this = this;

    this.node.removeAllChildren();
    this.pysf = this.PinyinSpriteFrames[this.spriteframeGroup];

    if (this.pysf === undefined) {
      cc.error('no spriteframe group');
      return;
    }

    arr.forEach(function (e) {
      var arr_letter = e.split('');
      arr_letter.forEach(function (w) {
        var node = _this.spriteBuilder(w);

        if (node !== null) _this.node.addChild(node);
      });
    });
  },
  spriteBuilder: function spriteBuilder(word) {
    var newnode = new cc.Node();
    var sprite = newnode.addComponent(cc.Sprite);
    sprite.type = cc.Sprite.Type.SIMPLE;
    sprite.sizeMode = cc.Sprite.SizeMode.RAW;
    sprite.trim = false;

    if (phonetic_symbol[word] !== undefined) {
      var ws = phonetic_symbol[word].split('');

      try {
        if (ws[0] === 'i') ws[0] = 'i0';
        sprite.spriteFrame = this.pysf.getSpriteFrame(this.spriteframe_prefix + ws[0]);
      } catch (e) {
        cc.error('no' + this.spriteframe_prefix + ws[0]);
        return null;
      }

      var tone = new cc.Node();
      var tone_sprite = tone.addComponent(cc.Sprite);
      tone_sprite.type = cc.Sprite.Type.SIMPLE;
      tone_sprite.sizeMode = cc.Sprite.SizeMode.RAW;
      tone_sprite.trim = false;

      try {
        tone_sprite.spriteFrame = this.pysf.getSpriteFrame(this.spriteframe_prefix + 'tone' + ws[1]);
        newnode.addChild(tone);

        if (sprite.spriteFrame === undefined) {
          cc.error('no   ' + this.spriteframe_prefix + 'tone' + ws[1]);
          return null;
        }
      } catch (e) {
        cc.error('error');
        return null;
      }

      if (ws[0] === 'v') {
        tone.y += tone.height * (1 / 7);
      }
    } else {
      try {
        sprite.spriteFrame = this.pysf.getSpriteFrame(this.spriteframe_prefix + word);

        if (sprite.spriteFrame === undefined) {
          cc.error('no   ' + this.spriteframe_prefix + word);
          return null;
        }
      } catch (e) {
        cc.error('error');
        return null;
      }
    }

    return newnode;
  },
  updateLabel: function updateLabel() {
    if (!this.layout) {
      cc.error('Failed to layout sprite, layout component is invalid!');
      return;
    }

    if (this.PinyinSpriteFrames.length < 1) {
      cc.error('At least one PinyinSpriteFrames');
      return;
    }

    var s = [];
    py(this.pinyinString).forEach(function (e) {
      s.push(e[0]);
    });
    this.resetSpriteRender(s);
  }
});

cc._RF.pop();